package xcmg.device.service.split;

import com.github.pagehelper.PageInfo;
import yb.ecp.fast.infra.util.PageHelperPlus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.barcode.BarcodeManageDO;
import xcmg.device.dao.entity.split.RestoreDO;
import xcmg.device.dao.entity.split.RestoreDetailDO;
import xcmg.device.dao.entity.split.SplitDO;
import xcmg.device.dao.entity.split.SplitDetailDO;
import xcmg.device.dao.entity.split.SplitRecordDO;
import xcmg.device.dao.entity.warehouse.WarehouseInventoryDO;
import xcmg.device.dao.mapper.BarcodeManageMapper;
import xcmg.device.dao.mapper.split.RestoreDetailMapper;
import xcmg.device.dao.mapper.split.RestoreMapper;
import xcmg.device.dao.mapper.split.SplitDetailMapper;
import xcmg.device.dao.mapper.split.SplitMapper;
import xcmg.device.dao.mapper.split.SplitRecordMapper;
import xcmg.device.dao.mapper.warehouse.WarehouseInventoryMapper;
import xcmg.device.infra.DateUtil;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.ExportUtil;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.GenDocumentService;
import xcmg.device.service.barcode.BarcodeService;
import xcmg.device.service.vo.split.SplitAddVO;
import xcmg.device.service.vo.split.SplitArrivalVO;
import xcmg.device.service.vo.split.SplitDetailVO;
import xcmg.device.service.vo.split.SplitPdaVO;
import xcmg.device.service.vo.split.SplitRecordVO;
import xcmg.device.service.vo.split.SplitVO;
import xcmg.device.service.warehouse.CommonService;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.ListUtil;
import yb.ecp.fast.infra.util.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SplitService {

    @Autowired
    private SplitMapper splitMapper;

    @Autowired
    private SplitDetailMapper splitDetailMapper;

    @Autowired
    private RestoreMapper restoreMapper;

    @Autowired
    private RestoreDetailMapper restoreDetailMapper;

    @Autowired
    private BarcodeManageMapper barcodeManageMapper;

    @Autowired
    private RestoreService restoreService;

    @Autowired
    private SplitRecordMapper splitRecordMapper;

    @Autowired
    private CommonService commonService;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private GenDocumentService genDocumentService;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private BarcodeService barcodeService;

    @Autowired
    private WarehouseInventoryMapper warehouseInventoryMapper;

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode add(SplitAddVO splitAddVO) throws Exception {
        if (splitAddVO.getHeaderInfo() == null || ListUtil.isNullOrEmpty(splitAddVO.getMaterialList())) {
            return ErrorCode.IllegalArument;
        }
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode()) {
            return ErrorCode.IllegalArument;
        }

        SplitDO splitDO = new SplitDO();
        BeanUtils.copyProperties(splitAddVO.getHeaderInfo(), splitDO);
        splitDO.setId(textResult.getValue());
        splitDO.setSplitOrder(genDocumentService.getDocumentNo(BillDesc.CMSplit));
        splitDO.setCreateDate(new Date());
        boolean success;

        for (SplitDetailVO detailVO : splitAddVO.getMaterialList()) {
            SplitDetailDO detailDO = new SplitDetailDO();
            BeanUtils.copyProperties(detailVO, detailDO);
            detailDO.setId(fastGenClient.textGuid().getValue());
            detailDO.setMainId(splitDO.getId());
            success = splitDetailMapper.insert(detailDO) > 0;
            if (!success) {
                throw new Exception("操作失败");
            }
        }
        success = splitMapper.insert(splitDO) > 0;
        if (!success) {
            throw new Exception("操作失败");
        }

        //新增拆件恢复单
        splitAddVO.getHeaderInfo().setSplitOrder(splitDO.getSplitOrder());
        success = restoreService.add(splitAddVO).getCode() == 0;
        if (!success) {
            throw new Exception("新增拆件恢复单失败");
        }
        return ErrorCode.Success;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode update(SplitAddVO splitAddVO) throws Exception {
        if (splitAddVO.getHeaderInfo() == null || ListUtil.isNullOrEmpty(splitAddVO.getMaterialList())) {
            return ErrorCode.IllegalArument;
        }
        SplitVO splitVO = splitAddVO.getHeaderInfo();
        if ((splitVO.getSplitType() == 0 && splitVO.getAuditStatus() == 5) ||
                (splitVO.getSplitType() == 1 && splitVO.getAuditStatus() == 7)) {
            throw new Exception("该拆件单已经审批通过，无法修改");
        }
        SplitDO splitDO = new SplitDO();
        BeanUtils.copyProperties(splitAddVO.getHeaderInfo(), splitDO);
        boolean success = splitDetailMapper.deleteByMainId(splitDO.getId()) > 0;
        if (!success) {
            throw new Exception("操作失败");
        }
        BigDecimal restoreAmount = BigDecimal.ZERO;
        for (SplitDetailVO detailVO : splitAddVO.getMaterialList()) {
            SplitDetailDO detailDO = new SplitDetailDO();
            BeanUtils.copyProperties(detailVO, detailDO);
            if (StringUtil.isNullOrEmpty(detailDO.getId())) {
                detailDO.setId(fastGenClient.textGuid().getValue());
            }
            detailDO.setMainId(splitDO.getId());
            success = splitDetailMapper.insert(detailDO) > 0;
            if (!success) {
                throw new Exception("操作失败");
            }
            if (detailDO.getMatType() == 0) {
                restoreAmount = detailDO.getAmount();
            }
        }
        switch (splitVO.getAuditStatus()) {
            case 2:
                splitDO.setAuditStatus(0);
                break;
            case 4:
                splitDO.setAuditStatus(1);
                break;
            case 6:
                splitDO.setAuditStatus(3);
                break;
            case 8:
                splitDO.setAuditStatus(5);
                break;
        }
        success = splitMapper.update(splitDO) > 0;
        if (!success) {
            throw new Exception("操作失败");
        }

        //更新拆件恢复单计划恢复数量
        RestoreDO restoreDO = restoreMapper.selectBySplitOrder(splitDO.getSplitOrder(), splitDO.getOrgId());
        if (restoreDO == null) {
            throw new Exception("未找到拆件恢复单");
        }
        restoreDO.setAmount(restoreAmount);
        success = restoreMapper.update(restoreDO) > 0;
        if (!success) {
            throw new Exception("操作失败");
        }
        return ErrorCode.Success;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode splitPda(SplitPdaVO splitPdaVO, String userId, String orgId) throws Exception {
        if (StringUtil.isNullOrEmpty(splitPdaVO.getSplitOrder()) || StringUtil.isNullOrEmpty(splitPdaVO.getSourceBarcode())
                || StringUtil.isNullOrEmpty(splitPdaVO.getSplitBarcode())) {
            return ErrorCode.IllegalArument;
        }
        if (splitPdaVO.getSourceAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new Exception("原件拆件数量必须大于0");
        }

        //判断拆件记录是否已存在
        SplitRecordVO recordCondition = new SplitRecordVO();
        recordCondition.setSplitOrder(splitPdaVO.getSplitOrder());
        recordCondition.setOrgId(orgId);
        recordCondition.setSourceBarcode(splitPdaVO.getSourceBarcode());
        recordCondition.setSplitBarcode(splitPdaVO.getSplitBarcode());
        if (!ListUtil.isNullOrEmpty(splitRecordMapper.list(recordCondition))) {
            throw new Exception("拆件记录已存在");
        }

        SplitDO splitDO = splitMapper.selectBySplitOrder(splitPdaVO.getSplitOrder(), orgId);
        if (splitDO == null) {
            throw new Exception("未找到拆件单");
        }
        if ((splitDO.getSplitType() == 0 && splitDO.getAuditStatus() != 5) || (splitDO.getSplitType() == 1 && splitDO.getAuditStatus() != 7)) {
            throw new Exception("拆件单尚未审核通过");
        }
        if (splitDO.getSplitStatus() == 2) {
            throw new Exception("拆件单已拆件完成");
        }
       /* int num = splitMapper.checkForSource(splitPdaVO.getSplitOrder(), splitPdaVO.getSourceBarcode(), orgId);
        if(num > 0){
            throw new Exception("原件条码已在其他拆件单中使用");
        }
        num = splitMapper.checkForSplit(splitPdaVO.getSplitOrder(), splitPdaVO.getSplitBarcode(), orgId);
        if(num > 0){
            throw new Exception("拆件条码已在其他拆件单中使用");
        }*/

        BarcodeManageDO sourceBarcode = barcodeManageMapper.item(splitPdaVO.getSourceBarcode(), orgId);
        if (sourceBarcode == null) {
            throw new Exception("未找到原件条码");
        }
        //扫描入库时如果实际入库数量小于条码数量，更新条码数量
        if (sourceBarcode.getCurrentQty().compareTo(splitPdaVO.getSourceAmount()) > 0) {
            barcodeService.updateBarcodeNum(sourceBarcode, splitPdaVO.getSourceAmount(), orgId);
        }
        BarcodeManageDO splitBarcode = barcodeManageMapper.item(splitPdaVO.getSplitBarcode(), orgId);
        if (splitBarcode == null) {
            throw new Exception("未找到拆件条码");
        }

        //更新拆件单中原件的已拆件数量
        boolean success;
        SplitDetailDO sourceDetail = splitDetailMapper.selectByMaterialNo(splitDO.getId(), 0, splitPdaVO.getSourceMaterialsNo());
        if (sourceDetail == null) {
            throw new Exception("未找到拆件单中原件记录");
        }
        RestoreDO restoreDO = restoreMapper.selectBySplitOrder(splitPdaVO.getSplitOrder(), orgId);
        if (restoreDO == null) {
            throw new Exception("未找到拆件恢复单");
        }
        RestoreDetailDO restoreDetailDO = restoreDetailMapper.selectByBarcode(restoreDO.getId(), splitPdaVO.getSourceBarcode());
        if (restoreDetailDO != null) {
            if (restoreDetailDO.getAmount().compareTo(splitPdaVO.getSourceAmount()) != 0) {
                throw new Exception("原件条码已被拆件，数量为" + restoreDetailDO.getAmount() + "，数量需保持一致");
            }
        } else {
            sourceDetail.setSplitAmount(sourceDetail.getSplitAmount().add(splitPdaVO.getSourceAmount()));
            if (sourceDetail.getSplitAmount().compareTo(sourceDetail.getAmount()) > 0) {
                throw new Exception("拆件单中原件已拆件数量不能大于计划拆件数量");
            }
            success = splitDetailMapper.update(sourceDetail) > 0;
            if (!success) {
                throw new Exception("更新拆件单中原件已拆件数量");
            }
        }

        //更新拆件单中拆件的已拆件数量
        SplitDetailDO splitDetail = splitDetailMapper.selectByMaterialNo(splitDO.getId(), 1, splitPdaVO.getSplitMaterialsNo());
        if (splitDetail == null) {
            throw new Exception("未找到拆件单中拆件记录");
        }
        splitDetail.setSplitAmount(splitDetail.getSplitAmount().add(splitPdaVO.getSplitAmount()));
        if (splitDetail.getSplitAmount().compareTo(splitDetail.getAmount()) > 0) {
            throw new Exception("拆件单中拆件已拆件数量不能大于计划拆件数量");
        }
        success = splitDetailMapper.update(splitDetail) > 0;
        if (!success) {
            throw new Exception("更新拆件单中拆件已拆件数量失败");
        }

        //更新拆件单主表拆件状态
        int num = splitDetailMapper.countSplitStatus(splitDO.getId());
        if (num == 0) {
            splitDO.setSplitStatus(2);
        } else {
            splitDO.setSplitStatus(1);
        }
        success = splitMapper.update(splitDO) > 0;
        if (!success) {
            throw new Exception("更新拆件单中拆件状态失败");
        }

        //新增拆件记录
        SplitRecordDO recordDO = new SplitRecordDO();
        recordDO.setId(fastGenClient.textGuid().getValue());
        recordDO.setSplitOrder(splitPdaVO.getSplitOrder());
        recordDO.setSourceBarcode(splitPdaVO.getSourceBarcode());
        recordDO.setSourceMaterialsNo(sourceDetail.getMaterialsNo());
        recordDO.setSourceMaterialsDes(sourceDetail.getMaterialsDes());
        recordDO.setSourceAmount(splitPdaVO.getSourceAmount());
        recordDO.setSplitBarcode(splitPdaVO.getSplitBarcode());
        recordDO.setSplitMaterialsNo(splitDetail.getMaterialsNo());
        recordDO.setSplitMaterialsDes(splitDetail.getMaterialsDes());
        recordDO.setSplitAmount(splitPdaVO.getSplitAmount());
        recordDO.setCreateId(userId);
        recordDO.setCreateName(commonService.getUserName(userId));
        recordDO.setCreateDate(new Date());
        recordDO.setOrgId(orgId);
        success = splitRecordMapper.insert(recordDO) > 0;
        if (!success) {
            throw new Exception("新增拆件记录失败");
        }

        //新增拆件恢复单待恢复记录
        success = restoreService.addDetail(splitPdaVO.getSplitOrder(), splitPdaVO.getSourceBarcode(), userId, orgId);
        if (!success) {
            throw new Exception("新增拆件恢复单待恢复记录失败");
        }

        //将库存中对应的原件条码状态改为已冻结
        WarehouseInventoryDO inventoryDO = warehouseInventoryMapper.selectByBarCode(splitPdaVO.getSourceBarcode(), orgId);
        if (inventoryDO == null) {
            throw new Exception("未在库存中找到对应的原件条码");
        }
       /* if(inventoryDO.getStatus() == 1){
            throw new Exception("原件条码已被冻结，无法拆件");
        }*/
        inventoryDO.setStatus(1);
        success = warehouseInventoryMapper.updateByPrimaryKeySelective(inventoryDO) > 0;
        if (!success) {
            throw new Exception("更新原件条码冻结状态失败");
        }
        return ErrorCode.Success;
    }

    public PageCommonVO list(SearchCommonVO<SplitVO> condition) {
        PageCommonVO<SplitVO> pageCommonVO = new PageCommonVO();
        PageHelperPlus.orderBy("create_date desc");
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<SplitDO> doList = splitMapper.list(condition.getFilters());
        List<SplitVO> voList = new ArrayList<>();
        for (SplitDO entity : doList) {
            SplitVO model = new SplitVO();
            BeanUtils.copyProperties(entity, model);
            voList.add(model);
        }
        pageCommonVO.setPageInfo(new PageInfo(doList));
        pageCommonVO.setPageInfoList(voList);
        return pageCommonVO;
    }

    public SplitVO getSplitAmount(SplitVO condition, String orgId) {
        SplitVO splitVO = new SplitVO();
        SplitDO splitDO = splitMapper.selectBySplitOrder(condition.getSplitOrder(), orgId);
        if (splitDO == null || (splitDO.getSplitType() == 0 && splitDO.getAuditStatus() != 5) ||
                (splitDO.getSplitType() == 1 && splitDO.getAuditStatus() != 7)) {
            return splitVO;
        }
        Map<String, BigDecimal> map = splitDetailMapper.countSplitAmount(condition.getSplitOrder(), 0, orgId);
        splitVO.setSourcePlanAmount(map.get("AMOUNT") == null ? BigDecimal.ZERO : map.get("AMOUNT"));
        splitVO.setSourceRealAmount(map.get("REAL_AMOUNT") == null ? BigDecimal.ZERO : map.get("REAL_AMOUNT"));
        map = splitDetailMapper.countSplitAmount(condition.getSplitOrder(), 1, orgId);
        splitVO.setSplitPlanAmount(map.get("AMOUNT") == null ? BigDecimal.ZERO : map.get("AMOUNT"));
        splitVO.setSplitRealAmount(map.get("REAL_AMOUNT") == null ? BigDecimal.ZERO : map.get("REAL_AMOUNT"));
        return splitVO;
    }

    public List<SplitDetailDO> detailList(SplitVO condition, String orgId) {
        return splitDetailMapper.listByMatType(condition.getSplitOrder(), condition.getMatType(), orgId);
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode delete(List<String> ids, String orgId) throws Exception {
        int num = splitMapper.findForDeleteByIds(ids);
        if (num > 0) {
            throw new Exception("已经开始审核的拆件单不能删除");
        }
        boolean success = splitMapper.updateDel(ids) > 0;
        if (!success) {
            throw new Exception("删除失败");
        }
        restoreMapper.updateDel(orgId, ids);
        return ErrorCode.Success;
    }

    @Transactional(rollbackFor = Exception.class)
    public ErrorCode audit(SplitVO splitVO, String userId, String userName) throws Exception {
        SplitDO entity = splitMapper.selectByPrimaryKey(splitVO.getId());
        if (entity == null) {
            return ErrorCode.IllegalArument;
        }
        if (entity.getAuditStatus() == 0 || entity.getAuditStatus() == 2) {
            if (splitVO.getStatus() == 1) {
                entity.setAuditStatus(1);
            } else {
                entity.setAuditStatus(2);
            }
            entity.setFirstAuditId(userId);
            entity.setFirstAuditName(userName);
            entity.setFirstAuditNote(splitVO.getRemark());
            entity.setFirstAuditDate(new Date());
        } else if (entity.getAuditStatus() == 1 || entity.getAuditStatus() == 4) {
            if (splitVO.getStatus() == 1) {
                entity.setAuditStatus(3);
            } else {
                entity.setAuditStatus(4);
            }
            entity.setSecondAuditId(userId);
            entity.setSecondAuditName(userName);
            entity.setSecondAuditNote(splitVO.getRemark());
            entity.setSecondAuditDate(new Date());
        } else if (entity.getAuditStatus() == 3 || entity.getAuditStatus() == 6) {
            if (splitVO.getStatus() == 1) {
                entity.setAuditStatus(5);
            } else {
                entity.setAuditStatus(6);
            }
            entity.setThirdAuditId(userId);
            entity.setThirdAuditName(userName);
            entity.setThirdAuditNote(splitVO.getRemark());
            entity.setThirdAuditDate(new Date());
        } else if (entity.getSplitType() == 1 && (entity.getAuditStatus() == 5 || entity.getAuditStatus() == 8)) {
            if (splitVO.getStatus() == 1) {
                entity.setAuditStatus(7);
            } else {
                entity.setAuditStatus(8);
            }
            entity.setFourthAuditId(userId);
            entity.setFourthAuditName(userName);
            entity.setFourthAuditNote(splitVO.getRemark());
            entity.setFourthAuditDate(new Date());
        }
        return splitMapper.update(entity) > 0 ? ErrorCode.Success : ErrorCode.Failure;
    }

    public SplitAddVO item(String id) {
        SplitAddVO result = new SplitAddVO();
        SplitDO splitDO = splitMapper.selectByPrimaryKey(id);
        if (splitDO == null) {
            return result;
        }
        SplitVO splitVO = new SplitVO();
        BeanUtils.copyProperties(splitDO, splitVO);
        if (splitVO.getAuditStatus() < 3) {
            splitVO.setRemark(splitVO.getFirstAuditNote());
        } else if (splitVO.getAuditStatus() < 5) {
            splitVO.setRemark(splitVO.getSecondAuditNote());
        } else if (splitVO.getAuditStatus() < 7) {
            splitVO.setRemark(splitVO.getThirdAuditNote());
        } else {
            splitVO.setRemark(splitVO.getFourthAuditNote());
        }
        result.setHeaderInfo(splitVO);
        List<SplitDetailVO> listVO = new ArrayList<>();
        List<SplitDetailDO> listDO = splitDetailMapper.list(id);
        for (SplitDetailDO entity : listDO) {
            SplitDetailVO model = new SplitDetailVO();
            BeanUtils.copyProperties(entity, model);
            model.setMatTypeDesc(model.getMatType() == 0 ? "原件" : "拆件");
            listVO.add(model);
        }
        result.setMaterialList(listVO);
        return result;
    }

    public void export(List<String> ids) throws Exception {
        PageHelperPlus.orderBy("create_date desc");
        invokeExport(splitMapper.findByIds(ids));
    }

    public void exportAll(SplitVO condition) throws Exception {
        List<SplitDO> dataList = new ArrayList<>();
        PageHelperPlus.orderBy("create_date desc");
        dataList.addAll(splitMapper.exportList(condition));
        invokeExport(dataList);
    }

    private void invokeExport(List<SplitDO> doList) throws Exception {
        String title = "拆件单数据导出";
        String[] rowsName = new String[]{"序号", "拆件单号", "拆件类型", "拆件状态", "原件恢复状态", "审核状态",
                "备注", "创建人", "创建时间","零件号","零件名称","零件类型","单位","数量","已入库数量","已出库数量","原零件编号","原零件名称"};
        List<Object[]> dataList = new ArrayList<>();
        Object[] objs;
        for (int i = 0; i < doList.size(); i++) {
            SplitDO exportDO = doList.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i + 1;
            objs[1] = exportDO.getSplitOrder();
            objs[2] = exportDO.getSplitType() == 0 ? "总成件" : "整机";
            objs[3] = getSplitState(exportDO.getSplitStatus());
            objs[4] = getRrestoreState(exportDO.getRestoreStatus());
            objs[5] = getAuditState(exportDO.getAuditStatus());
            objs[6] = exportDO.getNote();
            objs[7] = exportDO.getCreateName();
            objs[8] = DateUtil.formatDateTime(exportDO.getCreateDate());
            objs[9] = exportDO.getMaterialsNo();
            objs[10] = exportDO.getMaterialsDes();
            objs[11] = exportDO.getMaterialsType();
            objs[12] = exportDO.getUnit();
            objs[13] = exportDO.getAmount();
            objs[14] = exportDO.getInAmount();
            objs[15] = exportDO.getOutAmount();
            objs[16] = exportDO.getOriginMaterialsNo();
            objs[17] = exportDO.getOriginMaterialsDes();
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }

    private String getSplitState(Integer state) {
        switch (state) {
            case 0:
                return "未开始";
            case 1:
                return "部分拆件";
            case 2:
                return "已拆件";
            default:
                return "未开始";
        }
    }

    private String getRrestoreState(Integer state) {
        switch (state) {
            case 0:
                return "未恢复";
            case 1:
                return "部分恢复";
            case 2:
                return "已恢复";
            default:
                return "未恢复";
        }
    }

    private String getAuditState(Integer state) {
        switch (state) {
            case 0:
                return "待审核";
            case 1:
                return "第一步审核通过";
            case 2:
                return "第一步审核拒绝";
            case 3:
                return "第二步审核通过";
            case 4:
                return "第二步审核拒绝";
            case 5:
                return "第三步审核通过";
            case 6:
                return "第三步审核拒绝";
            case 7:
                return "第四步审核通过";
            case 8:
                return "第四步审核拒绝";
            default:
                return "待审核";
        }
    }

    /**
     * @Author: WangKe
     * @Description: 更新拆件单拆件详情的已入库数量和已出库数量
     * @Date: 2018/9/10 0010
     */
    public boolean updateInOutNum(String splitOrder, String materialsNo, BigDecimal amount, Integer type, String orgId) {
        SplitDO splitDO = splitMapper.selectBySplitOrder(splitOrder, orgId);
        if (splitDO == null) {
            return false;
        }
        SplitDetailDO splitDetailDO = splitDetailMapper.selectByMaterialNo(splitDO.getId(), 1, materialsNo);
        if (splitDetailDO == null) {
            return false;
        }
        if (type == 1) {
            //入库时
            splitDetailDO.setInAmount(splitDetailDO.getInAmount().add(amount));
        } else {
            //出库时
            splitDetailDO.setOutAmount(splitDetailDO.getOutAmount().add(amount));
        }
        return splitDetailMapper.update(splitDetailDO) > 0;
    }

    public PageCommonVO arrivalList(SearchCommonVO<SplitArrivalVO> condition) {
        PageCommonVO<SplitArrivalVO> pageCommonVO = new PageCommonVO();
        PageHelperPlus.startPage(condition.getPageNum(), condition.getPageSize());
        List<SplitArrivalVO> voList = splitDetailMapper.arrivalList(condition.getFilters());
        pageCommonVO.setPageInfo(new PageInfo(voList));
        pageCommonVO.setPageInfoList(voList);
        return pageCommonVO;
    }

    public void exportArrivalAll(SplitArrivalVO condition) throws Exception {
        Integer count = splitDetailMapper.arrivalListCount(condition);
        List<SplitArrivalVO> list = new ArrayList<>();
        int times = count / 500 + 1;
        for (int i = 0; i < times; i++) {
            PageHelperPlus.startPage(i + 1, 500);
            list.addAll(splitDetailMapper.arrivalList(condition));
        }

        String title = "拆件到货数据导出";
        String[] rowsName = new String[]{"序号", "拆件单号", "零件编码", "零件名称", "零件类型", "单位",
                "数量", "已拆件数量", "计划内仓库库存", "创建时间"};
        List<Object[]> dataList = new ArrayList<>();
        Object[] objs;
        for (int i = 0; i < list.size(); i++) {
            SplitArrivalVO exportDO = list.get(i);
            objs = new Object[rowsName.length];
            objs[0] = i + 1;
            objs[1] = exportDO.getSplitOrder();
            objs[2] = exportDO.getMaterialsNo();
            objs[3] = exportDO.getMaterialsDes();
            objs[4] = exportDO.getMaterialsType();
            objs[5] = exportDO.getUnit();
            objs[6] = exportDO.getAmount();
            objs[7] = exportDO.getSplitAmount();
            objs[8] = exportDO.getInQty();
            objs[9] = DateUtil.formatDateTime(exportDO.getCreateDate());
            dataList.add(objs);
        }
        ExportUtil exportUtil = new ExportUtil(title, rowsName, dataList);
        exportUtil.export(response);
    }

}
